/**
 * Created by pankaj on 26/10/14.
 */
define(['jquery'],function($){

    var DropDown = function(parentDom, dataFuture){
        var thisObj = this;
        this.index = -1;
        this.parentDom = parentDom;

        dataFuture.then(function(menuData){
            parentDom.find('[role=menu]').empty();
            menuData.forEach(function(eachData, index){
                parentDom.find('[role=menu]').
                    append('<li><a href="javascript:" data-index="'+eachData.id+'">'+eachData.description+'</a></li>');
            });
            thisObj.index = menuData[1].id;
            parentDom.find('.btn-group .selection').text(menuData[1].description);
            parentDom.find(".dropdown-menu li a[data-index="+menuData[1].id+"]").addClass('btn-info');
            parentDom.find(".dropdown-menu li a").once('click',function(){
                changeIndex($(this));
            });
        });



        var changeIndex = function(clickedA){
            thisObj.index = clickedA.attr('data-index');
            clickedA.parents(".btn-group").find('.selection').text(clickedA.text());
           // clickedA.parents(".btn-group").find('.selection').val(clickedA.text());
            parentDom.find(".dropdown-menu li a").each(function(index, obj){
                $(this).removeClass('btn-info');
            });
            clickedA.addClass('btn-info');
        };

        this.reset = function(){
            changeIndex(this.parentDom.find(".dropdown-menu li a:first"));
        };

        this.getValue = function(){
            return thisObj.index;
        }
    };

    return DropDown;
});
